//
//  STSettingViewController.swift
//  SeaTransportManage
//
//  Created by guopengwen on 2021/11/26.
//

import UIKit

class STSettingViewController: GPBaseViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    var dataSource: Array = ["关于我们", "隐私说明", "修改密码", "版本信息"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
       setNavBarTitle("系统设置")
        
        view.addSubview(listView)
        let h = relativeHeight(w: 343, h: 202)
        listView.snp.makeConstraints { (make) in
            make.top.equalTo(20)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(h)
        }
        
        STSettingCollectionViewCell.registerTo(listView)
    }
    
    
    // UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataSource.count
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let h = 50 * (kScreenWidth - 32) / 343
        return CGSize.init(width: kScreenWidth - 32, height: h)
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = STSettingCollectionViewCell.reusableCell(collectionView, indexPath: indexPath)
        cell?.updateCellData(title: dataSource[indexPath.item], indexPath.item)
        return cell ?? collectionView.dequeueReusableCell(withReuseIdentifier: STSettingCollectionViewCell.identifier, for: indexPath)
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if (indexPath.item == 0) {
            let vc = GPWebViewController.init()
            vc.url = "\(kServiceHost)/html/about.html"
            vc.pageTitle = "关于我们"
            navigationController?.pushViewController(vc, animated: true)
        } else if (indexPath.item == 1) {
            let vc = GPWebViewController.init()
            vc.url = "\(kServiceHost)/html/privacy.html"
            vc.pageTitle = "隐私说明"
            navigationController?.pushViewController(vc, animated: true)
        } else if (indexPath.item == 2) {
            let vc = STModifyPasswordViewController.init()
            navigationController?.pushViewController(vc, animated: true)
        }
    }
    
    lazy var listView: UICollectionView = {
        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout.init()
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        let collectView: UICollectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: layout)
        collectView.delegate = self
        collectView.dataSource = self
        collectView.backgroundColor = UIColor.white
        collectView.layer.cornerRadius = 8
        return collectView
    }()
}
